package com.ruoyi.erp.service.impl;

import java.lang.reflect.GenericDeclaration;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.erp.domain.*;
import com.ruoyi.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ProjectHourAbnormalMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 异常工时Service业务层处理
 * 
 * @author sauger
 * @date 2022-04-13
 */
@Service
public class ProjectHourAbnormalServiceImpl implements IProjectHourAbnormalService 
{
    @Autowired
    private ProjectHourAbnormalMapper projectHourAbnormalMapper;
    @Autowired
    private IProjectHourService projectHourService;

    /**
     * 查询异常工时
     * 
     * @param id 异常工时主键
     * @return 异常工时
     */
    @Override
    public ProjectHourAbnormal selectProjectHourAbnormalById(Integer id)
    {
        return projectHourAbnormalMapper.selectProjectHourAbnormalById(id);
    }

    /**
     * 查询异常工时列表
     * 
     * @param projectHourAbnormal 异常工时
     * @return 异常工时
     */
    @Override
    public List<ProjectHourAbnormal> selectProjectHourAbnormalList(ProjectHourAbnormal projectHourAbnormal)
    {
        return projectHourAbnormalMapper.selectProjectHourAbnormalList(projectHourAbnormal);
    }

    /**
     * 新增异常工时
     * 
     * @param projectHourAbnormal 异常工时
     * @return 结果
     */
    @Override
    public int insertProjectHourAbnormal(ProjectHourAbnormal projectHourAbnormal)
    {
        return projectHourAbnormalMapper.insertProjectHourAbnormal(projectHourAbnormal);
    }

    /**
     * 修改异常工时
     * 
     * @param projectHourAbnormal 异常工时
     * @return 结果
     */
    @Override
    public int updateProjectHourAbnormal(ProjectHourAbnormal projectHourAbnormal)
    {
        return projectHourAbnormalMapper.updateProjectHourAbnormal(projectHourAbnormal);
    }

    /**
     * 批量删除异常工时
     * 
     * @param ids 需要删除的异常工时主键
     * @return 结果
     */
    @Override
    public int deleteProjectHourAbnormalByIds(String ids)
    {
        return projectHourAbnormalMapper.deleteProjectHourAbnormalByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除异常工时信息
     * 
     * @param id 异常工时主键
     * @return 结果
     */
    @Override
    public int deleteProjectHourAbnormalById(Integer id)
    {
        return projectHourAbnormalMapper.deleteProjectHourAbnormalById(id);
    }

    /**
     * 计算指定时间段内异常工时
     * @param holidayConfigTypeId
     * @param beginDate
     * @param endDate
     */
    @Override
    @Transactional
    public void calculateProjectHourAbnormal(Integer holidayConfigTypeId, Date beginDate, Date endDate) {
        List<DateWorkHour> dateWorkHourList = projectHourService.getAllDateWorkHours(holidayConfigTypeId, beginDate, endDate);
        //删除已存在的异常
        projectHourAbnormalMapper.deleteProjectHourAbnormalByDuration(beginDate,endDate);
        dateWorkHourList.stream().forEach(dateWorkHour -> {
            if ((dateWorkHour.getDateType().equals("workday") || dateWorkHour.getDateType().equals("overtime")) && dateWorkHour.getNeedHour().compareTo(dateWorkHour.getWorkHour())>0){
                ProjectHourAbnormal abnormal = new ProjectHourAbnormal();
                abnormal.setAbnormalDate(dateWorkHour.getWorkDate());
                abnormal.setAbnormalType("4");
                abnormal.setUserId(dateWorkHour.getUserId());
                abnormal.setLessHour(dateWorkHour.getNeedHour().subtract(dateWorkHour.getWorkHour()));
                abnormal.setDescription(dateWorkHour.getDateType().equals("workday") ? "工作日工时不满" : "加班工时不满");
                projectHourAbnormalMapper.insertProjectHourAbnormal(abnormal);
            }
        });
    }


    @Override
    public void calculateProjectHourAbnormal(Integer holidayConfigTypeId, Date beginDate) {
        this.calculateProjectHourAbnormal(holidayConfigTypeId, beginDate, beginDate);
    }

    /**
     * 汇总时间段内的异常信息
     *
     * @param projectHourAbnormal
     * @return
     */
    @Override
    public ProjectHourAbnormal groupProjectHourAbnormal(ProjectHourAbnormal projectHourAbnormal) {
        return projectHourAbnormalMapper.groupProjectHourAbnormal(projectHourAbnormal);
    }
}
